package io.wdb.modules.generator.generator.service.impl.book;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.wdb.common.utils.Query;
import io.wdb.modules.generator.generator.dao.BookTopicalMappingsDao;
import io.wdb.modules.generator.generator.entity.BookTopicalMappingsEntity;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.book.BookSubjectService;

@Service
public class BookSubjectServiceImpl implements BookSubjectService{

	@Autowired
	private BookTopicalMappingsDao bookTopicalMappingsDao;
	
	@Override
	public List<BookTopicalMappingsEntity> queryList(Query query) {
		return bookTopicalMappingsDao.queryList(query);
	}

	@Override
	public int queryTotal(Query query) {
		return bookTopicalMappingsDao.queryTotal(query);
	}

	@Override
	public WebResponse updateSubject(BookTopicalMappingsEntity bookTopicalMappingsEntity) {
		WebResponse webResponse = new WebResponse();
		BookTopicalMappingsEntity bookTopicalMappingsEntityDb = bookTopicalMappingsDao.queryObject(bookTopicalMappingsEntity.getId());
		bookTopicalMappingsEntityDb.setDescription(bookTopicalMappingsEntity.getDescription());
		//bookTopicalMappingsEntityDb.setTopicalCode(topicalCode);
		bookTopicalMappingsEntityDb.setTopicalName(bookTopicalMappingsEntity.getTopicalName());
		bookTopicalMappingsDao.update(bookTopicalMappingsEntityDb);
		return webResponse;
	}

	@Override
	public WebResponse insertSubject(BookTopicalMappingsEntity bookTopicalMappingsEntity) {
		WebResponse webResponse = new WebResponse();
		Integer maxTagCode = bookTopicalMappingsDao.selectMaxTopicalCode();
		if(bookTopicalMappingsEntity.getTopicalCode()==null||"".equals(bookTopicalMappingsEntity.getTopicalCode())) {
			String maxTagCodeStr = maxTagCode+"";
			int length = maxTagCodeStr.length();
			for(int i=0;i<4-length;i++) {
				maxTagCodeStr="0"+maxTagCodeStr;
			}
			bookTopicalMappingsEntity.setTopicalCode(maxTagCodeStr.toString());
		}
		bookTopicalMappingsDao.save(bookTopicalMappingsEntity);
		return null;
	}

	@Override
	public WebResponse deleteSubject(BookTopicalMappingsEntity bookTopicalMappingsEntity) {
		WebResponse webResponse = new WebResponse();
		bookTopicalMappingsDao.delete(bookTopicalMappingsEntity.getId());
		return webResponse;
	}

}
